<?php
namespace Trawind\Skeleton\Generators;

use Illuminate\Support\Str;
use Prettus\Repository\Generators\Migrations\SchemaParser;
use Prettus\Repository\Generators\ValidatorGenerator;

/**
 * Class RepositoryEloquentGenerator
 * @package Prettus\Repository\Generators
 * @author Anderson Andrade <contato@andersonandra.de>
 */
class RemoteRepositoryEloquentGenerator extends Generator
{

    /**
     * Get stub name.
     *
     * @var string
     */
    protected $stub = 'remote/eloquent';


    /**
     * Get generator path config node.
     *
     * @return string
     */
    public function getPathConfigNode()
    {
        return 'repositories';
    }

    /**
     * Get destination path for generated file.
     *
     * @return string
     */
    public function getPath()
    {
        return $this->getBasePath() . '/Remotes/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . '/' . $this->getName() . 'RepositoryEloquent.php';
    }

    /**
     * Get base path of destination file.
     *
     * @return string
     */
    public function getBasePath()
    {
        return config('repository.generator.basePath', app()->path());
    }

    /**
     * Get array replacements.
     *
     * @return array
     */
    public function getReplacements()
    {
        $interfaces = parent::getRootNamespace() . parent::getConfigGeneratorClassPath('interfaces') . '\\' . $this->name . 'Repository;';
        $interfaces= str_replace('TrawindCloud','Trawind\\Remotes',$interfaces);

        $interfaces = str_replace([
            "\\",
            '/'
        ], '\\', $interfaces);

        
        return array_merge(parent::getReplacements(), [
            'interfaces'    => $interfaces,
            'domain'=>strtoupper(env('APP_NAME')),
            'url'=>$this->getUrl()
        ]);
    }

    
    
    public function getUrl()
    {
        $url='';
        foreach (explode('/',$this->getName()) as $item){
            $url.='/'.strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '-', $item));
        }
        return Str::plural($url);
    }


    public function getNamespace()
    {
        $namespace=parent::getNamespace();
        return str_replace('TrawindCloud','Trawind\\Remotes',$namespace);
    }

    /**
     * Get root namespace.
     *
     * @return string
     */
    public function getRootNamespace()
    {
        $root_namespace=parent::getRootNamespace() . parent::getConfigGeneratorClassPath($this->getPathConfigNode());
        return str_replace('TrawindCloud','Trawind\\Remotes',$root_namespace);
    }
}
